import JSEncrypt from 'jsencrypt/bin/jsencrypt.min'

import { sm2 } from 'sm-crypto'

// 密钥对生成 http://web.chacuo.net/netrsakeypair

const publicKey =
  'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdH\n' +
  'nzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ=='

const privateKey =
  'MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAqhHyZfSsYourNxaY\n' +
  '7Nt+PrgrxkiA50efORdI5U5lsW79MmFnusUA355oaSXcLhu5xxB38SMSyP2KvuKN\n' +
  'PuH3owIDAQABAkAfoiLyL+Z4lf4Myxk6xUDgLaWGximj20CUf+5BKKnlrK+Ed8gA\n' +
  'kM0HqoTt2UZwA5E2MzS4EI2gjfQhz5X28uqxAiEA3wNFxfrCZlSZHb0gn2zDpWow\n' +
  'cSxQAgiCstxGUoOqlW8CIQDDOerGKH5OmCJ4Z21v+F25WaHYPxCFMvwxpcw99Ecv\n' +
  'DQIgIdhDTIqD2jfYjPTY8Jj3EDGPbH2HHuffvflECt3Ek60CIQCFRlCkHpi7hthh\n' +
  'YhovyloRYsM+IS9h/0BzlEAuO0ktMQIgSPT3aFAgJYwKpqRYKlLDVcflZFCKY7u3\n' +
  'UP8iWi1Qw0Y='

// sm2公钥
let smPublicKey =
  '04c40816baec8eee1604ce222bac71050fd7315d6b2e30f34da861b3be228c3c3be55bba8263d51703e308a5f46b2e4ec275d30aa72d23214dcee442a649b27f9a'

// if (import.meta.env.VITE_APP_ENV === 'development' || import.meta.env.VITE_APP_ENV === 'test') {
//   smPublicKey =
//     '04c40816baec8eee1604ce222bac71050fd7315d6b2e30f34da861b3be228c3c3be55bba8263d51703e308a5f46b2e4ec275d30aa72d23214dcee442a649b27f9a'
// } else if (import.meta.env.VITE_APP_ENV === 'production') {
  // smPublicKey =
  //   '0459f9d7e2e73306af3d4fb8b1cec5a2e01d9970af75c3c1022d69ccb3dab8cafc5fb3cf494d0793de0b2db341aa2c14369531d4397fb3c7b94d96bc2d4267b70a'
// }
// 加密
export function encrypt(txt) {
  const encryptor = new JSEncrypt()
  encryptor.setPublicKey(publicKey) // 设置公钥
  return encryptor.encrypt(txt) // 对数据进行加密
}

// // 解密
// export function decrypt(txt) {
//   const encryptor = new JSEncrypt()
//   encryptor.setPrivateKey(privateKey) // 设置私钥
//   return encryptor.decrypt(txt) // 对数据进行解密
// }


// 修改RSA加密函数，支持分段加密
// export function encrypt(txt) {
//   const encryptor = new JSEncrypt()
//   encryptor.setPublicKey(publicKey)
  
//   // RSA 512位密钥最大加密长度为53字节
//   const maxLength = 53
//   const data = txt.toString()
  
//   if (data.length <= maxLength) {
//     return encryptor.encrypt(data)
//   }
  
//   // 分段加密
//   let encrypted = ''
//   for (let i = 0; i < data.length; i += maxLength) {
//     const chunk = data.substring(i, i + maxLength)
//     const encryptedChunk = encryptor.encrypt(chunk)
//     if (!encryptedChunk) {
//       throw new Error('加密失败')
//     }
//     encrypted += encryptedChunk + '|' // 使用分隔符
//   }
  
//   return encrypted.slice(0, -1) // 移除最后的分隔符
// }

// // 对应的解密函数
// export function decrypt(txt) {
//   const encryptor = new JSEncrypt()
//   encryptor.setPrivateKey(privateKey)
  
//   // 检查是否是分段加密的数据
//   if (txt.includes('|')) {
//     const chunks = txt.split('|')
//     let decrypted = ''
    
//     for (const chunk of chunks) {
//       const decryptedChunk = encryptor.decrypt(chunk)
//       if (decryptedChunk === false) {
//         throw new Error('解密失败')
//       }
//       decrypted += decryptedChunk
//     }
    
//     return decrypted
//   } else {
//     return encryptor.decrypt(txt)
//   }
// }



export function smEncrypto(val) {
  //加解密
  const cipherMode = 1 // 1 - C1C3C2，0 - C1C2C3，默认为1
  var encryptData = sm2.doEncrypt(val, smPublicKey, cipherMode) // 加密结果
  var data = encryptData.substring(0, 2) == '04' ? encryptData : '04' + encryptData
  return data
}
